package ui;
import javax.imageio.ImageIO;
import javax.swing.*;
import org.jfree.*;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;

import system.Algorithm;
import system.NmfAlg;
import system.RandIndex;

import java.awt.*;
import java.io.File;
import java.io.IOException;
public class ResultsUI extends JFrame{

	
	public static JTextArea textPane;
	public static JTable table;
	public static JLabel lblNumberOfGroups;
	public static JLabel lblRandIndexValue;
	
		public ResultsUI() {
			
		       Image image = null;
			try {
				image = ImageIO.read(new File("images\\body.jpg"));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
				this.setContentPane(new ImagePanel(image));
			
			setResizable(false);
			setSize(new Dimension(800, 600));
			getContentPane().setLayout(null);
			
			JLabel lblResults = new JLabel("Results:");
			lblResults.setFont(new Font("MV Boli", Font.BOLD, 30));
			lblResults.setForeground(Color.DARK_GRAY);
			lblResults.setBounds(10, 11, 139, 23);
			getContentPane().add(lblResults);
			
			JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
			tabbedPane.setSize(new Dimension(764, 580));
			tabbedPane.setBounds(10, 44, 764, 500);
			getContentPane().add(tabbedPane);
			
			
			JPanel panelNMF = new JPanel();

			tabbedPane.addTab("Pie Chart", null, panelNMF, null);
			tabbedPane.setFont(new Font("MV Boli", Font.BOLD, 20));
			JPanel panelResults=new JPanel();
			tabbedPane.addTab("Algorithm Result",null,panelResults,null );
			panelResults.setLayout(null);
		
		    JScrollPane scrollPane_1 = new JScrollPane();
		    scrollPane_1.setAutoscrolls(true);
		    scrollPane_1.setBounds(new Rectangle(0, 0, 627, 350));
		    scrollPane_1.setBounds(72, 83, 627, 352);
		    panelResults.add(scrollPane_1);
		    table=new JTable(RandIndex.data,RandIndex.Groups);
		    table.setBounds(new Rectangle(0, 0, 627, 350));
		    table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		    table.setFont(new Font("Tahoma", Font.BOLD, 13));
		    scrollPane_1.setViewportView(table);
		    table.setPreferredScrollableViewportSize(new Dimension(350, 70));
		    table.setFillsViewportHeight(true);
			table.setEnabled(false);
			table.setRowSelectionAllowed(false);
			
			lblNumberOfGroups = new JLabel("Number of groups:");
			lblNumberOfGroups.setForeground(new Color(139, 69, 19));
			lblNumberOfGroups.setFont(new Font("MV Boli", Font.BOLD, 14));
			lblNumberOfGroups.setVisible(false);
			lblNumberOfGroups.setBounds(71, 58, 221, 24);
			panelResults.add(lblNumberOfGroups);
			
			lblRandIndexValue = new JLabel("Rand Index value:");
			lblRandIndexValue.setForeground(new Color(139, 69, 19));
			lblRandIndexValue.setFont(new Font("MV Boli", Font.BOLD, 14));
			lblRandIndexValue.setVisible(false);
			lblRandIndexValue.setBounds(478, 58, 221, 24);
			panelResults.add(lblRandIndexValue);
			
			JLabel lblNewLabel = new JLabel("Algorithm Results:");
			lblNewLabel.setForeground(new Color(139, 69, 19));
			lblNewLabel.setFont(new Font("MV Boli", Font.BOLD, 26));
			lblNewLabel.setBounds(203, 11, 496, 36);
			panelResults.add(lblNewLabel);
			
			
			JPanel panelNMFlog=new JPanel();
			tabbedPane.addTab("Log", null, panelNMFlog,null);

			panelNMFlog.setLayout(null);
			
			JScrollPane scrollPane = new JScrollPane();
			scrollPane.setBounds(0, 0, 759, 472);
			panelNMFlog.add(scrollPane);
			
			textPane = new JTextArea();
			textPane.setEditable(false);
			textPane.setDisabledTextColor(Color.BLACK);
			scrollPane.setViewportView(textPane);
			textPane.setText(NmfAlg.log);

			
			DefaultPieDataset nmfPieChart = new DefaultPieDataset();
			if(MainMenuUI.X.alg==1){
				for(int i=0;i<NmfAlg.k;i++)
				{
					nmfPieChart.setValue("Group "+i,NmfAlg.clusterList.get(i));
				}
				
				lblNumberOfGroups.setText("Number of groups: "+MainMenuUI.textField.getText());
				lblRandIndexValue.setText("Rand Index value: "+RandIndex.result);
				lblNumberOfGroups.setVisible(true);
				lblRandIndexValue.setVisible(true);
			}
			else
			{
				
				//nmfPieChart.setValue("Cluster +");
				for(int i=0;i<Algorithm.clusters;i++)
				{
					nmfPieChart.setValue("Group "+(i+1),Algorithm.col_sum.get(Algorithm.index)[i]);
					lblNumberOfGroups.setText("Number of groups: "+Algorithm.clusters);
					lblRandIndexValue.setText("Rand Index value: "+Algorithm.bestRandIndexValue);
					lblNumberOfGroups.setVisible(true);
					lblRandIndexValue.setVisible(true);
				}

			}
			

			
			
			JFreeChart chart1=ChartFactory.createPieChart3D("Pie Chart",nmfPieChart,true,true,false);
			
			PiePlot3D plot3 = (PiePlot3D) chart1.getPlot();
	        plot3.setForegroundAlpha(0.6f);
	        plot3.setCircular(true);
	        
	        panelNMF.add(new ChartPanel(chart1));
			
			
		}
}


